
#include "highlighter.h"


Highlighter::Highlighter(QTextDocument *parent)
    : QSyntaxHighlighter(parent)
{
    HighlightingRule rule;

    keywordFormat.setForeground(Qt::darkBlue);
    keywordFormat.setFontWeight(QFont::Bold);
    QStringList keywordPatterns;
    keywordPatterns << "\\bentero\\b" << "\\barreglo\\b" << "\\bcadena\\b"
                    << "\\breal\\b" << "\\bbooleano\\b" << "\\bcaracter\\b"
                    << "\\bregistro\\b"<< "\\bfuncion\\b"<< "\\bprocedimiento\\b"
                    << "\\bentrada\\b"<< "\\bsalida\\b"<< "\\bbajo\\b"<< "\\balto\\b"
                    << "\\bpin\\b";
    foreach (const QString &pattern, keywordPatterns) {
        rule.pattern = QRegExp(pattern,Qt::CaseInsensitive);
        rule.format = keywordFormat;
        highlightingRules.append(rule);

    }

    sentenciaFormat.setForeground(Qt::magenta);
    QStringList sentenciaPatterns;
    sentenciaPatterns << "\\bpara\\b" << "\\bhasta\\b" << "\\bmientras\\b"
                      << "\\bsi\\b" << "\\brepita\\b" << "\\bcaso\\b"<< "\\bllamar\\b"
                      << "\\bescriba\\b"<< "\\blea\\b"<< "\\bsino\\b" << "\\bentonces\\b"
                      << "\\bhaga\\b"<< "\\bmodo\\b";
    foreach (const QString &pattern, sentenciaPatterns) {
        rule.pattern = QRegExp(pattern,Qt::CaseInsensitive);
        rule.format = sentenciaFormat;
        highlightingRules.append(rule);

    }

    iniciofinFormat.setForeground(Qt::darkGreen);
    QStringList iniciofinPatterns;
    iniciofinPatterns << "\\binicio\\b" << "\\bfin\\b";
    foreach (const QString &pattern, iniciofinPatterns) {
        rule.pattern = QRegExp(pattern,Qt::CaseInsensitive);
        rule.format = iniciofinFormat;
        highlightingRules.append(rule);

    }


    singleLineCommentFormat.setForeground(Qt::green);
    rule.pattern = QRegExp("//[^\n]*");
    rule.format = singleLineCommentFormat;
    highlightingRules.append(rule);

    multiLineCommentFormat.setForeground(Qt::green);

    quotationFormat.setForeground(Qt::red);
    rule.pattern = QRegExp("\"[^\"\r]*\"");
    rule.format = quotationFormat;
    highlightingRules.append(rule);

    singlequotationFormat.setForeground(Qt::red);
    rule.pattern = QRegExp("\'.\'");
    rule.format = singlequotationFormat;
    highlightingRules.append(rule);

    functionFormat.setFontItalic(true);
    functionFormat.setForeground(Qt::blue);
    rule.pattern = QRegExp("\\b[A-Za-z0-9_]+[ ]*(?=\\()");
    rule.format = functionFormat;
    highlightingRules.append(rule);

    commentStartExpression = QRegExp("/\\*");
    commentEndExpression = QRegExp("\\*/");
}

void Highlighter::highlightBlock(const QString &text)
{
    foreach (const HighlightingRule &rule, highlightingRules) {
        QRegExp expression(rule.pattern);


        int index = expression.indexIn(text);
        while (index >= 0) {
            int length = expression.matchedLength();
            setFormat(index, length, rule.format);
            index = expression.indexIn(text, index + length);
        }
    }

    setCurrentBlockState(0);

    int startIndex = 0;
    if (previousBlockState() != 1)
        startIndex = commentStartExpression.indexIn(text);


    while (startIndex >= 0) {

        int endIndex = commentEndExpression.indexIn(text, startIndex);
        int commentLength;
        if (endIndex == -1) {
            setCurrentBlockState(1);
            commentLength = text.length() - startIndex;
        } else {
            commentLength = endIndex - startIndex
                    + commentEndExpression.matchedLength();
        }
        setFormat(startIndex, commentLength, multiLineCommentFormat);
        startIndex = commentStartExpression.indexIn(text, startIndex + commentLength);
    }
}
//! [11]
